package com.zhuangjie.coupon.controller;

import com.zhaungjie.base.conduit.R;
import com.zhuangjie.base.constant.ModuleSecurityNames;
import com.zhuangjie.coupon.dto.LockCouponsDTO;
import com.zhuangjie.coupon.service.CouponService;
import com.zhuangjie.coupon.vo.ApplicableShopCouponCoreInfoVO;
import com.zhuangjie.security.annotations.OnlySystemCall;
import com.zhuangjie.security.annotations.PublicAPI;
import com.zhuangjie.security.provider.UserProvider;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhuangjie
 * @since 2024-02-28
 */
@RestController
@RequestMapping("/coupon")
public class CouponController {
    @Autowired
    private CouponService couponService;


    @ApiOperation("优惠卷膨胀")
    @PostMapping("/expand-coupon/{couponId}")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    public R expandCoupon(@PathVariable("couponId") Long couponId) {
        return R.capture(()->couponService.expandCoupon(couponId));
    }

    @ApiOperation("有效优惠卷统计")
    @GetMapping("/valid-coupon-count")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    public R validCouponCount() {
        return R.require(couponService.validCouponCount(UserProvider.currentUserId()));
    }

    @ApiOperation("查询当前用户已领取与可领取（仅店铺券）的券")
    @GetMapping("/applicable-shop-coupon/{shopId}")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    public R queryApplicableShopCoupon(@NotNull(message = "店铺id不能为空") @PathVariable("shopId") Long shopId) {
        return R.require(couponService.queryApplicableCouponsForShop(UserProvider.currentUserId(),shopId));
    }

    /**
     * 返回完整Coupon再加部分必要的CouponBatch必要属性
     * 传入shop是因为，该接口可能会用于选择店铺优惠券，如果店铺可能不支持神券，那显示的券不应该是神券了，应降级为普通券
     * @return
     */
    @ApiOperation("查询我的平台优惠券")
    @GetMapping("/my-platform-coupons")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    public R myPlatformCoupons(@RequestParam(value = "shopId",required = false) Long shopId) throws ExecutionException, InterruptedException {
        return R.require(couponService.queryMyPlatformCoupons(true,UserProvider.currentUserId(),shopId));
    }



    @ApiOperation("查询当前用户已领取的平台券-（店铺页-调用）")
    @GetMapping("/received-platform-coupon")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    public R queryReceivedPlatformCoupon(@RequestParam("isParticipatingInPlatformInflationCouponActivity") Boolean isParticipatingInPlatformInflationCouponActivity) {
        return R.require(couponService.queryReceivedPlatformCoupon(UserProvider.currentUserId(),isParticipatingInPlatformInflationCouponActivity));
    }



    @ApiOperation("查询当前用户已领取/可领取的店铺券/平台券”-core信息-不折叠（确认页请求）")
    @GetMapping("/applicable-shop-coupon-core-info")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    public R queryApplicableShopCouponCoreInfo(@NotNull(message = "店铺id不能为空") @RequestParam("shopId") Long shopId,
                                               @NotNull(message = "店铺是否支持神会员不能为空") @RequestParam("isParticipatingInPlatformInflationCouponActivity") Boolean isParticipatingInPlatformInflationCouponActivity) {
        return R.require(ApplicableShopCouponCoreInfoVO.class,couponService.queryApplicableShopCouponCoreInfo(shopId,isParticipatingInPlatformInflationCouponActivity));
    }



    @ApiOperation("查询用户可领取/已拥有的指定店铺优惠券")
    @GetMapping("/applicable-coupons-for-shop/{shopId}")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    public R queryApplicableCouponsForShop(@NotNull(message = "店铺id不能为空") @PathVariable("shopId") Long shopId) {
        return R.require(List.class,couponService.queryApplicableCouponsForShop(UserProvider.currentUserId(), shopId));
    }

    @ApiOperation("查询用户拥有的所有有效的店铺券")
    @GetMapping("/all-available-shop-coupons")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    public R queryAllAvailableShopCoupons(@RequestParam(value = "shopId",required = false) Long shopId) throws Exception {
        return R.require(List.class,couponService.queryAllAvailableShopCoupons(shopId));
    }

    @GetMapping("/receive-by")
    @ApiOperation("查询指定batchId列表的券信息-用于使用券页面券的领取")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    public R queryCouponForReceive(@NotNull(message = "需要shopId") @RequestParam("shopId") Long shopId,
                                   @NotEmpty(message = "batchId不能为空") @RequestParam("batchIds") List<Long> batchIds) throws Exception {
        return R.require(List.class,couponService.queryShopsDisplayInfo(shopId,batchIds));
    }


    @ApiOperation("优惠券锁定-仅内部调用接口")
    @PutMapping("/lock-coupons")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    @OnlySystemCall
    public R lockCoupons(@Valid @RequestBody LockCouponsDTO lockCouponsDTO) {
        return R.capture(()->couponService.lockCoupons(lockCouponsDTO));
    }

    @ApiOperation("取消锁定退还优惠券-仅内部调用接口")
    @PutMapping("/un-lock-coupons")
    @PublicAPI
    @OnlySystemCall
    public R unLockCoupons(@Valid @RequestBody LockCouponsDTO lockCouponsDTO) {
        return R.capture(()->couponService.unLockCoupons(lockCouponsDTO));
    }





}
